import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.functions.Index;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2020/10/20 9:58
 */
public class CreateWorkBook {
    public static void main(String[] args) throws IOException {
        List<Roler> list = new ArrayList<>(){{
            add(new Roler("琴","主c/辅助","身为西风骑士团的代理团长，琴一直忠于职守，为人们带来安宁。\n虽然并非天赋异禀，但通过刻苦训练，如今的她已然能够独当一面。"));
            add(new Roler("芭芭拉","辅助/战斗牧师","既是西风教会的祈礼牧师，又是蒙德城的闪耀偶像。\n对于习惯了传统吟游诗人的蒙德来说，「偶像」是还不习惯的新生事物。\n但在蒙德，人人都爱芭芭拉。这就是自由之都的自由精神——对大家投来的喜爱，芭芭拉是这样满怀感激地总结的。"));
            add(new Roler("可莉","主c","西风骑士团，火花骑士！永远伴随闪光与爆炸出现！\n" +
                    "——然后在琴团长严厉的目光注视下默默消失。"));
        }};
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("测试的表");

        //设置了风格
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //设置字体
        HSSFFont font = (HSSFFont) workbook.createFont();
        font.setBold(true);
        font.setColor(IndexedColors.BLUE.getIndex());
        style.setFont(font);
        font.setFontHeight((short) 500);




        //这里没有做判断，因为即使是没有做判断，文件也会被创建
        File file = new File("D:/a.xls");

        try ( FileOutputStream outputStream = new FileOutputStream(file)) {
            Sheet sheet1 = workbook.createSheet("第一个表");

            Sheet sheet2 = workbook.createSheet("第二个表");

            Row row = sheet1.createRow(0);
            row.createCell(0).setCellValue("人物");
            row.createCell(1).setCellValue("定位");
            row.createCell(2).setCellValue("描述");

            for (int i = 0; i < list.size(); i++) {

                Row newRow = sheet1.createRow(i + 1);
                Cell cell = newRow.createCell(0);
                cell.setCellStyle(style);
                cell.setCellValue(list.get(i).getName());

                Cell cell1 = newRow.createCell(1);
                cell1.setCellStyle(style);
                cell1.setCellValue(list.get(i).getPosition());


                Cell cell2 = newRow.createCell(2);
                cell2.setCellStyle(style);
                cell2.setCellValue(list.get(i).getDescription());
            }

            //最后一定要写入
            workbook.write(outputStream);
        }

        System.out.println("写入完成，请到"+file.getAbsolutePath()+"查看");


    }
}
